﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for frmNghiviec.
	/// </summary>
	public class frmNghiviec : System.Windows.Forms.Form
	{
        private AccessData mDB = new AccessData();
        Language lan = new Language();
		private DataSet ds=new DataSet();
        int flag = 0;
		private System.Windows.Forms.DataGrid dgrDanhMuc7;
		private System.Windows.Forms.TextBox txtTen;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtMa;
		private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Panel panel1;
        private Button butKetthuc;
        private Button butHuy;
        private Button butBoqua;
        private Button butLuu;
        private Button butSua;
        private Button butThem;
        private Panel panel2;
        private CheckBox chkThoiviec;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmNghiviec()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmNghiviec));
            this.dgrDanhMuc7 = new System.Windows.Forms.DataGrid();
            this.txtTen = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtMa = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butThem = new System.Windows.Forms.Button();
            this.panel2 = new System.Windows.Forms.Panel();
            this.chkThoiviec = new System.Windows.Forms.CheckBox();
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc7)).BeginInit();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // dgrDanhMuc7
            // 
            this.dgrDanhMuc7.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dgrDanhMuc7.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc7.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dgrDanhMuc7.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dgrDanhMuc7.CaptionBackColor = System.Drawing.Color.LightSteelBlue;
            this.dgrDanhMuc7.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc7.CaptionVisible = false;
            this.dgrDanhMuc7.DataMember = "";
            this.dgrDanhMuc7.FlatMode = true;
            this.dgrDanhMuc7.Font = new System.Drawing.Font("Tahoma", 8F);
            this.dgrDanhMuc7.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc7.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc7.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dgrDanhMuc7.HeaderBackColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc7.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dgrDanhMuc7.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc7.LinkColor = System.Drawing.Color.Teal;
            this.dgrDanhMuc7.Location = new System.Drawing.Point(3, 3);
            this.dgrDanhMuc7.Name = "dgrDanhMuc7";
            this.dgrDanhMuc7.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dgrDanhMuc7.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dgrDanhMuc7.RowHeaderWidth = 10;
            this.dgrDanhMuc7.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dgrDanhMuc7.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dgrDanhMuc7.Size = new System.Drawing.Size(576, 302);
            this.dgrDanhMuc7.TabIndex = 1;
            this.dgrDanhMuc7.CurrentCellChanged += new System.EventHandler(this.dgrDanhMuc7_CurrentCellChanged);
            // 
            // txtTen
            // 
            this.txtTen.BackColor = System.Drawing.Color.White;
            this.txtTen.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtTen.Location = new System.Drawing.Point(158, 11);
            this.txtTen.MaxLength = 50;
            this.txtTen.Name = "txtTen";
            this.txtTen.Size = new System.Drawing.Size(310, 20);
            this.txtTen.TabIndex = 1;
            this.txtTen.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtTen_KeyDown);
            // 
            // label2
            // 
            this.label2.BackColor = System.Drawing.SystemColors.Control;
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(119, 14);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(39, 13);
            this.label2.TabIndex = 5;
            this.label2.Text = "Lý do :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txtMa
            // 
            this.txtMa.BackColor = System.Drawing.Color.White;
            this.txtMa.Enabled = false;
            this.txtMa.ForeColor = System.Drawing.Color.DarkBlue;
            this.txtMa.Location = new System.Drawing.Point(72, 11);
            this.txtMa.MaxLength = 2;
            this.txtMa.Name = "txtMa";
            this.txtMa.Size = new System.Drawing.Size(44, 20);
            this.txtMa.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.SystemColors.Control;
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(21, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(53, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "Mã lý do :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.butKetthuc);
            this.panel1.Controls.Add(this.butHuy);
            this.panel1.Controls.Add(this.butBoqua);
            this.panel1.Controls.Add(this.butLuu);
            this.panel1.Controls.Add(this.butSua);
            this.panel1.Controls.Add(this.butThem);
            this.panel1.Location = new System.Drawing.Point(43, 50);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(497, 39);
            this.panel1.TabIndex = 3;
            // 
            // butKetthuc
            // 
            this.butKetthuc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKetthuc.ForeColor = System.Drawing.Color.DarkBlue;
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(384, 5);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(65, 28);
            this.butKetthuc.TabIndex = 5;
            this.butKetthuc.Text = "&Thoát";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.UseVisualStyleBackColor = true;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butHuy
            // 
            this.butHuy.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butHuy.ForeColor = System.Drawing.Color.DarkBlue;
            this.butHuy.Image = ((System.Drawing.Image)(resources.GetObject("butHuy.Image")));
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(313, 5);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(65, 28);
            this.butHuy.TabIndex = 4;
            this.butHuy.Text = "&Hủy";
            this.butHuy.UseVisualStyleBackColor = true;
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butBoqua.ForeColor = System.Drawing.Color.DarkBlue;
            this.butBoqua.Image = ((System.Drawing.Image)(resources.GetObject("butBoqua.Image")));
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(237, 5);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(70, 28);
            this.butBoqua.TabIndex = 3;
            this.butBoqua.Text = "  &Bỏ qua";
            this.butBoqua.UseVisualStyleBackColor = true;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // butLuu
            // 
            this.butLuu.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butLuu.ForeColor = System.Drawing.Color.DarkBlue;
            this.butLuu.Image = ((System.Drawing.Image)(resources.GetObject("butLuu.Image")));
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(166, 5);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(65, 28);
            this.butLuu.TabIndex = 2;
            this.butLuu.Text = "&Lưu";
            this.butLuu.UseVisualStyleBackColor = true;
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butSua
            // 
            this.butSua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butSua.ForeColor = System.Drawing.Color.DarkBlue;
            this.butSua.Image = ((System.Drawing.Image)(resources.GetObject("butSua.Image")));
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(95, 5);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(65, 28);
            this.butSua.TabIndex = 1;
            this.butSua.Text = "&Sửa";
            this.butSua.UseVisualStyleBackColor = true;
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butThem
            // 
            this.butThem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butThem.ForeColor = System.Drawing.Color.DarkBlue;
            this.butThem.Image = ((System.Drawing.Image)(resources.GetObject("butThem.Image")));
            this.butThem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThem.Location = new System.Drawing.Point(24, 5);
            this.butThem.Name = "butThem";
            this.butThem.Size = new System.Drawing.Size(65, 28);
            this.butThem.TabIndex = 0;
            this.butThem.Text = "&Mới";
            this.butThem.UseVisualStyleBackColor = true;
            this.butThem.Click += new System.EventHandler(this.butThem_Click);
            // 
            // panel2
            // 
            this.panel2.BackColor = System.Drawing.SystemColors.Control;
            this.panel2.Controls.Add(this.chkThoiviec);
            this.panel2.Controls.Add(this.txtTen);
            this.panel2.Controls.Add(this.panel1);
            this.panel2.Controls.Add(this.txtMa);
            this.panel2.Controls.Add(this.label2);
            this.panel2.Controls.Add(this.label1);
            this.panel2.Location = new System.Drawing.Point(3, 309);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(576, 100);
            this.panel2.TabIndex = 0;
            // 
            // chkThoiviec
            // 
            this.chkThoiviec.ForeColor = System.Drawing.Color.DarkBlue;
            this.chkThoiviec.Location = new System.Drawing.Point(470, 13);
            this.chkThoiviec.Name = "chkThoiviec";
            this.chkThoiviec.Size = new System.Drawing.Size(70, 17);
            this.chkThoiviec.TabIndex = 2;
            this.chkThoiviec.Text = "Thôi việc";
            this.chkThoiviec.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chkThoiviec.UseVisualStyleBackColor = true;
            this.chkThoiviec.KeyDown += new System.Windows.Forms.KeyEventHandler(this.chkThoiviec_KeyDown);
            // 
            // frmNghiviec
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(583, 412);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.dgrDanhMuc7);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmNghiviec";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Lý do nghỉ việc";
            this.Load += new System.EventHandler(this.frmNghiviec_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dgrDanhMuc7)).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		private void frmNghiviec_Load(object sender, System.EventArgs e)
		{
			LoadGridTableStyle();
			enable_control(true);
			refresh_Text();
			enable_textbox(false);
			butThem.Focus();
		}

		private void LoadGridTableStyle()
		{
			DataSet ds1=new DataSet();
			ds1=mDB.get_data("select ma as heso,ten as diengiai,thoiviec,case when thoiviec=1 then 'Nghi' else '' end as ldthoiviec from "+mDB.user+".dmlydo order by ma");
			dgrDanhMuc7.DataSource=null;
			dgrDanhMuc7.TableStyles.Clear();
			DataGridTableStyle ts = new DataGridTableStyle();
			ts.MappingName = ds1.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "heso";
			TextCol1.HeaderText = lan.Change_language_MessageText("Mã lý do");
			TextCol1.Width = 80;
			TextCol1.ReadOnly=true;
			TextCol1.Alignment=HorizontalAlignment.Left;
			ts.GridColumnStyles.Add(TextCol1);
			dgrDanhMuc7.TableStyles.Add(ts);

            TextCol1 = new DataGridTextBoxColumn();
            TextCol1.MappingName = "diengiai";
            TextCol1.HeaderText = lan.Change_language_MessageText("Lý do");
            TextCol1.Width = 400;
            TextCol1.ReadOnly = true;
            TextCol1.Alignment = HorizontalAlignment.Left;
            ts.GridColumnStyles.Add(TextCol1);
			dgrDanhMuc7.TableStyles.Add(ts);

            TextCol1 = new DataGridTextBoxColumn();
            TextCol1.MappingName = "ldthoiviec";
            TextCol1.HeaderText = lan.Change_language_MessageText("Thôi việc");
            TextCol1.Width = 80;
            TextCol1.ReadOnly = true;
            TextCol1.Alignment = HorizontalAlignment.Left;
            ts.GridColumnStyles.Add(TextCol1);
            dgrDanhMuc7.TableStyles.Add(ts);

            ts.DataGrid.DataSource=ds1.Tables[0];
            CurrencyManager cm = (CurrencyManager)BindingContext[dgrDanhMuc7.DataSource, dgrDanhMuc7.DataMember];
            DataView dv = (DataView)cm.List;
            dv.AllowNew = false;
            dv.AllowEdit = false;
            dv.AllowDelete = false;
		}
        private string get_maxid()
        {
            int m_tam = 0;
            ds = mDB.get_data("SELECT Max(to_number(ma,9999)) FROM " + mDB.user + ".dmlydo");
            if (ds.Tables[0].Rows.Count > 0)
                m_tam = ds.Tables[0].Rows[0][0].ToString() !=""? int.Parse(ds.Tables[0].Rows[0][0].ToString()) + 1:1;
            else
                m_tam = 1;
            return m_tam.ToString().PadLeft(2, '0').ToString();	
        }
		private void butThem_Click(object sender, System.EventArgs e)
		{
            clear_Text();
			enable_control(false);
			enable_textbox(true);
            txtMa.Text = get_maxid();
            flag = 0;
			txtTen.Focus();
		}

		private void enable_control(bool ena)
		{
			butThem.Enabled=ena;
			butSua.Enabled=ena;
			butLuu.Enabled=!ena;
			butBoqua.Enabled=!ena;
			butHuy.Enabled=ena;
			butKetthuc.Enabled=ena;
		}
		
        private void enable_textbox(bool ena)
		{
            txtTen.Enabled = ena;
            chkThoiviec.Enabled = ena;
		}
		
        private void clear_Text()
		{			
			txtTen.Clear();
            chkThoiviec.Checked = false;
		}

		private void refresh_Text()
		{
            CurrencyManager cm = (CurrencyManager)BindingContext[dgrDanhMuc7.DataSource, dgrDanhMuc7.DataMember];
            DataView dv = (DataView)cm.List;
            dv.AllowNew = false;
            dv.AllowEdit = false;
            dv.AllowDelete = false;

            int i = dgrDanhMuc7.CurrentRowIndex;
			if(i>=0 && i<dv.Table.Rows.Count)
			{
                txtMa.Text = dv[i]["heso"].ToString();
                txtTen.Text = dv[i]["diengiai"].ToString();
                chkThoiviec.Checked = dv[i]["thoiviec"].ToString() == "1";
			}
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
            flag = 1;
			enable_control(false);
            enable_textbox(true);
			txtTen.Focus();
		}

        private bool kiemtra()
        {
            if (txtMa.Text.Trim() == ""||txtMa.Text.Trim().Length!=2)
            {
                MessageBox.Show(lan.Change_language_MessageText("Nhập mã lý do nghỉ việc!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtMa.Focus();
                return false;
            }
            if (txtTen.Text.Trim() == "")
            {
                MessageBox.Show(lan.Change_language_MessageText("Nhập tên lý do nghỉ việc!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtTen.Focus();
                return false;
            }
            return true;
        }

		private void butLuu_Click(object sender, System.EventArgs e)
		{
            int m_id = 0;
			try
			{
                string n_ma = "", n_ten = "";
                int n_thoiviec = 0;
                n_ma = txtMa.Text.Trim();
                n_ten = txtTen.Text.Trim();
                n_thoiviec = (chkThoiviec.Checked) ? 1 : 0;
                if (flag == 0)
                {
                    m_id = Int32.Parse(get_maxid());
                    txtMa.Text = m_id.ToString().PadLeft(3, '0');
                }
                else
                {
                    if (txtMa.Text != "")
                        m_id = Int32.Parse(txtMa.Text.ToString());
                }
                if (!mDB.upd_dmnghiviec(n_ma, n_ten, n_thoiviec))
                {
                    MessageBox.Show(lan.Change_language_MessageText("Không cập nhật thông tin nghỉ việc!"), AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    butBoqua.Focus();
                    return;
                }
				LoadGridTableStyle();
                flag = 0;

				enable_control(true);				
				enable_textbox(false);
				butThem.Focus();
			}
			catch{}
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			enable_control(true);
			txtTen.Enabled=false;
			refresh_Text();
			butKetthuc.Focus();
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			DialogResult dr = MessageBox.Show(lan.Change_language_MessageText("Đồng ý huỷ thông tin này?"),AccessData.Msg,MessageBoxButtons.YesNo);			
			if(dr == DialogResult.Yes) 			
			{
				mDB.get_data("delete from "+mDB.user+".dmlydo where ma='"+txtMa.Text.Trim()+"'");
			}
			LoadGridTableStyle();
			refresh_Text();	
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void dgrDanhMuc7_CurrentCellChanged(object sender, System.EventArgs e)
		{
			refresh_Text();
			enable_textbox(false);
			enable_control(true);
		}

		private void txtTen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
            if (e.KeyCode == Keys.Enter) 
                SendKeys.Send("{Tab}");
		}

        private void chkThoiviec_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }
	}
}
